{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "——————\n",
    "\n",
    "*日期：2023年3月20日\n",
    "\n",
    "*课程：python\n",
    "\n",
    "*记录人：林锦纯\n",
    "\n",
    "——————"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    ">1.人脸识别概念和基础\n",
    ">2.人脸识别应用\n",
    ">3.人脸识别实践-face++\n",
    ">>1.API文档阅读规范\n",
    ">>2.人脸检测（人脸分析）实践\n",
    ">>3.输入/输出\n",
    ">>4.错误码寻找分析"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 本周内容"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 1.人脸对比\n",
    "> 2.人脸搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 考试必考！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679281796,3b70fec1-8427-47c5-9615-971acc3ba794', 'time_used': 782, 'confidence': 75.808, 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}, 'faces1': [{'face_token': 'c3f196a93ee7b6463876d4bf6f090f24', 'face_rectangle': {'top': 525, 'left': 1037, 'width': 539, 'height': 539}}], 'faces2': [{'face_token': 'e7cd21733d739747d7bc2576682e3869', 'face_rectangle': {'top': 90, 'left': 58, 'width': 130, 'height': 130}}], 'image_id1': 'eYkCDV4JUwlBzyHsYOPa+g==', 'image_id2': 'pH3HSpKizuppQ8yb09OS7g=='}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸比对\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "# 二进制方式打开图片文件\n",
    "f_01 = open('zjl01.jpg', 'rb')\n",
    "f_02 = open('zjl02.png', 'rb')\n",
    "img_01= base64.b64encode(f_01.read())\n",
    "img_02 = base64.b64encode(f_02.read())\n",
    "\n",
    "params = {\n",
    "     'api_key':'Wkah6fZnkFvb-BNoq8fUMLkH_qZvUTcL',\n",
    "    'api_secret':'zWI1NiWGqaFWtfsjaSG8_rX7XfUmGYn5',\n",
    "    \"image_base64_1\":img_01,\n",
    "    \"image_base64_2\":img_02,\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print (response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.人脸搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.1人脸库管理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1.1创建人脸数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'faceset_token': 'c8a1b7ba77d5fdaa62600f96f359e298', 'time_used': 188, 'face_count': 0, 'face_added': 0, 'request_id': '1679283532,076b30ff-1556-400e-a030-d8420b1fecbb', 'outer_id': '', 'failure_detail': []}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "# 请求参数\n",
    "payload={\n",
    "    'api_key':'wPuNBlGVvsqTWQ7IW95jmcDtanc0Rh4b',\n",
    "    'api_secret':'v5B7Sshn5rESrVf1sr8WDfEFUYmYAJ9T',\n",
    "    \"display_name\":\"广州南方学院第1栋教学楼\"\n",
    "} \n",
    "\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "\n",
    "print (response.json())\n",
    "print(response. status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1.3增加人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "400\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'time_used': 197,\n",
       " 'error_message': 'INVALID_FACESET_TOKEN',\n",
       " 'request_id': '1679283703,c1e320b0-cd94-46ca-8774-a76f878dec39'}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "'''\n",
    "人脸数据集合详情查询\n",
    "'''\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "# 请求参数\n",
    "params={\n",
    "    \"api_key\":\"Wkah6fZnkFvb-BNoq8fUMLkH_qZvUTcL\",\n",
    "    \"api_secret\":\"zWI1NiWGqaFWtfsjaSG8_rX7XfUmGYn5\",\n",
    "    \"faceset_token\":\"c8a1b7ba77d5fdaa62600f96f359e298\"\n",
    "    \"face_tokens\":\"\"\n",
    "}\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print(response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1.4检查人脸集合的人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679307313,38002fc4-ebe8-4599-b91d-7edc1c95e842', 'time_used': 114, 'faces': [{'face_token': 'e48f0227b2f4b88d7511013c79bb5023', 'face_rectangle': {'top': 262, 'left': 248, 'width': 248, 'height': 248}}], 'image_id': 'x45psW3c7qQUtrp+rwdkcw==', 'face_num': 1}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "# 先检测人脸信息\n",
    "\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "人脸检测: 刘亦菲、蔡徐坤、高启强\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('1栋/lyf.jpg', 'rb')\n",
    "\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "\n",
    "params = {\n",
    "     \"api_key\":\"Wkah6fZnkFvb-BNoq8fUMLkH_qZvUTcL\",\n",
    "    \"api_secret\":\"zWI1NiWGqaFWtfsjaSG8_rX7XfUmGYn5\",\n",
    "    \"image_base64\":img,\n",
    "\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print (response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " *['7d2ef00e802300d8c6538511b4f2c684','675479a01a9a5779ae71ea8db08b189e','9d3219e623e96472d78a94de417ad77f']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '689f8e1cf460b1346a420f32deb2af15',\n",
       " 'time_used': 104,\n",
       " 'face_count': 3,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1679307368,142c7d45-27d5-4808-96c2-b2b72b62a635',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "'''\n",
    "增加人脸\n",
    "'''\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    \"api_key\":\"jRpCaZ34kqNYJ8Zppdc-yGGum_YkETov\",\n",
    "    \"api_secret\":\"Vr0PtRCw-ZFwYXTKVfi6aDNaSqlfunK3\",\n",
    "    \"faceset_token\":\"689f8e1cf460b1346a420f32deb2af15\",\n",
    "    \"face_tokens\":'9d3219e623e96472d78a94de417ad77f'\n",
    "\n",
    "}\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 人脸搜索（课后作业）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
